/*
 ============================================================================
 Name		: MessageSender.cpp
 Author	  : Lenka Litvova
 Version	 : 1.0
 Copyright   : Copyright belongs to Lenka Litvova
 Description : CMessageSender implementation
 ============================================================================
 */

#include "MessageSender.h"
#include <rsendasmessage.h> // RSendAsMessage
#include <senduiconsts.h> // RSendAsMessage
#include <bautils.h> //for BaflUtils, link against: bafl.lib
#include <COEMAIN.H> //for CCoeEnv, link against: cone.lib

CMessageSender::CMessageSender()
	{
	// No implementation required
	}

CMessageSender::~CMessageSender()
	{
		iServer.Close();
	}

CMessageSender* CMessageSender::NewLC()
	{
	CMessageSender* self = new (ELeave) CMessageSender();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CMessageSender* CMessageSender::NewL()
	{
	CMessageSender* self = CMessageSender::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void CMessageSender::ConstructL()
	{
		User::LeaveIfError(iServer.Connect());
	}

void CMessageSender::SendSmsL(const TDesC& aPhoneNumber, const TDesC& aMessage)
	{
		RSendAsMessage sms;
	    sms.CreateL(iServer, KSenduiMtmSmsUid);
	    CleanupClosePushL(sms);
	 
	    sms.AddRecipientL(aPhoneNumber, RSendAsMessage::ESendAsRecipientTo);
	    sms.SetBodyTextL(aMessage);
	 
	    TRequestStatus status;
	    sms.SendMessage(status);
	    User::WaitForRequest(status); //caka na na overenie ci bola sms dobre poslana
	    sms.Close();
	 
	    CleanupStack::Pop(&sms);
	}

void CMessageSender::SendMmsL(const TDesC& aPhoneNumber, const TDesC &aAttachment)
	{
		RSendAsMessage mms;
		mms.CreateL(iServer, KSenduiMtmMmsUid);
	    CleanupClosePushL(mms);
	 
	    mms.AddRecipientL(aPhoneNumber, RSendAsMessage::ESendAsRecipientTo);
	 
	    if(aAttachment.Length() > 0 && BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aAttachment))
	    {
	    	TRequestStatus status;
	    	mms.AddAttachment(aAttachment, status);
	    	User::WaitForRequest(status);
	    }
	    
	    TRequestStatus status;
	    mms.SendMessage(status);
	    User::WaitForRequest(status); //caka na na overenie ci bola sms dobre poslana
	    mms.Close();
	 
	    CleanupStack::Pop(&mms);
	}

void CMessageSender::SendMailL(const TDesC& aMail, const TDesC& aMessage, const TDesC &aAttachment)
	{
		RSendAsMessage mail;
		
		mail.CreateL(iServer, KSenduiMtmSmtpUid);
		CleanupClosePushL(mail);
		 
		mail.AddRecipientL(aMail, RSendAsMessage::ESendAsRecipientTo);
		mail.SetBodyTextL(aMessage);
		
		if(aAttachment.Length() > 0 && BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),aAttachment))
		{
			TRequestStatus status;
			mail.AddAttachment(aAttachment, status);
			User::WaitForRequest(status);
		}
		 
		TRequestStatus status;
		mail.SendMessage(status);
		User::WaitForRequest(status); //caka na na overenie ci bola sms dobre poslana
		mail.Close();
		 
		CleanupStack::Pop(&mail);
	}
